<?php

namespace App;

use Tymon\JWTAuth\Contracts\JWTSubject;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable implements JWTSubject
{
    use Notifiable;

    protected $table = 'users_basic';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */ 
    protected $fillable = [
        'account',
        'realname',
        'head',
        'password',
        'qq',
        'role_id',
        'sex',
        'telephone',
        'mobilephone',
        'email',
        'id_card',
        'weixin',
        'address',
        'status'
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token', 'deleted_at'
    ];

    /**
     * The accessors to append to the model's array form.
     *
     * @var array
     */
    protected $appends = ['head_img'];

    // Rest omitted for brevity

    /**
     * Get the identifier that will be stored in the subject claim of the JWT.
     *
     * @return mixed
     */
    public function getJWTIdentifier()
    {
        return $this->getKey();
    }

    /**
     * Return a key value array, containing any custom claims to be added to the JWT.
     *
     * @return array
     */
    public function getJWTCustomClaims()
    {
        return [];
    }

    /**
     * 读取角色
     * @return [type] [description]
     */
    public function roles(){
        return $this->belongsTo('App\Role','role_id','id');
    }

    /**
     * 登录日志
     * @return [type] [description]
     */
    public function log(){
        return $this->hasMany('App\UserLog','user_id','id');
    }

    /**
     * 获取封面图片。
     *
     * @param  string  $value
     * @return string
     */
    public function getHeadImgAttribute(){
        $value = $this->attributes['head'] ? asset($this->attributes['head']) : '';
        return $value;
    }
}
